package com.sharemob.tinchat.modules.launch;

import java.io.File;

import org.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.sharemob.tinchat.R;
import com.sharemob.tinchat.base.BaseActivityGroup;
import com.sharemob.tinchat.base.Callback;
import com.sharemob.tinchat.base.GooeyMenu;
import com.sharemob.tinchat.lib.Utils;
import com.sharemob.tinchat.lib.common.ExitHelper;
import com.sharemob.tinchat.lib.common.SMGlobal;
import com.sharemob.tinchat.modules.account.ActivityAccountInfo;
import com.sharemob.tinchat.modules.camera.CropPhotoActivity;
import com.sharemob.tinchat.modules.camera.LiveActivity;
import com.sharemob.tinchat.modules.camera.LiveCoverActivity;
import com.sharemob.tinchat.modules.camera.ReleaseVideoActivity;
import com.sharemob.tinchat.modules.camera.VideoActivity;
import com.sharemob.tinchat.modules.home.FragmentCenter;

public class MainActivity extends BaseActivityGroup implements GooeyMenu.GooeyMenuInterface{
	
	private static final int INDEX_CHATROOM = 0;
	private static final int INDEX_DISCOVER = 1;
	private static final int INDEX_FRIEND = 2;
	private static final int INDEX_CENTER = 3;
	private static final int INDEX_CAMERA=4;
	
	private static final int TAKE_PICTURE = 0x000001;
	private static final int  CROP_BIG_PICTURE=0x000002;
	
	private static final int[] MenuIDs=new int[]{R.id.main_tab_chatroom,R.id.main_tab_discover,R.id.main_tab_friend,R.id.main_tab_center,R.id.main_tab_camera};
	private int index =3;
    private GooeyMenu mGooeyMenu;
    
	public void initFragment(){
		this.addFragmentManager(fragmentManager.findFragmentById(R.id.fragement_chatroom));
		this.addFragmentManager(fragmentManager.findFragmentById(R.id.fragement_discover));
		this.addFragmentManager(fragmentManager.findFragmentById(R.id.fragement_friend));
		this.addFragmentManager(fragmentManager.findFragmentById(R.id.fragement_center));
		
		hideAllFragment();
		showFragment(INDEX_CENTER);
		
        RadioGroup rg = (RadioGroup) findViewById(R.id.main_tab_group);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            	ClickMenu();
            	switch (checkedId) {
				case R.id.main_tab_chatroom:
					 index =INDEX_CHATROOM;
					break;
				case R.id.main_tab_discover:
					index =INDEX_DISCOVER;
					break;
				case R.id.main_tab_friend:
					index =INDEX_FRIEND;
					break;
				case R.id.main_tab_center:
					index =INDEX_CENTER;
					break;
				case R.id.main_tab_camera:
					break;
				default:
					break;
				}
            	showFragment(index);
            }
        });
     for(int i=0;i<MenuIDs.length;i++){
 		BtnEvent(MenuIDs[i],new Callback() {
			public void doDown() {
				ClickMenu();
			}
		});
     }

	}
	
	private void ClickMenu(){
		if(mGooeyMenu.isMenuVisible){
			mGooeyMenu.ClickMainMenu();
		}
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SMGlobal.getInstance().context=this;
		
		initFragment();
		
	    mGooeyMenu = (GooeyMenu)findViewById(R.id.camera_main_menu);
        mGooeyMenu.setOnMenuListener(this);
        mGooeyMenu.bringToFront();
	}
    @Override
    public void menuOpen() {
        

    }

    public void menuClose() {
    	
    }

    private Uri photoPath_Uri=Utils.getCropPhotoFilePath();
    void Photo(){
    	Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    	intent.putExtra(MediaStore.EXTRA_OUTPUT,photoPath_Uri );
        startActivityForResult(intent, TAKE_PICTURE);
        
    }
    
    void Live(){
		  Intent intent = new Intent();
          intent.setClass(this,LiveCoverActivity.class);
          startActivity(intent);
          overridePendingTransition(R.anim.in_from_bottom,0);
    }
    
    void Video(){
	   	Intent intent = new Intent();
        intent.setClass(this,VideoActivity.class);
        startActivity(intent);
    	overridePendingTransition(R.anim.activity_open,0);
    	
    	
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case TAKE_PICTURE:
//				cropImageUri(photoPath_Uri, 100, 100, CROP_BIG_PICTURE);
				
			
//				break;
//				
//			case CROP_BIG_PICTURE:
			   	Intent intent = new Intent();
	            intent.setClass(this,CropPhotoActivity.class);
	            startActivity(intent);
	        	overridePendingTransition(R.anim.activity_open,0);
				break;
			}
		}
    	

	}
    public void menuItemClicked(int menuNumber) {
        mGooeyMenu.ClickMainMenu();
        switch (menuNumber) {
		case 1:
			Photo();
			break;
		case 2:
			Live();
			break;
		case 3:
			Video();
			break;
		default:
			break;
		}

    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {

            return ExitHelper.checkExit(this, keyCode);
    }
}
